home *** CD-ROM | disk | FTP | other *** search
- /*
- File: main.c
-
- Contains: This file is a shell that can be used to build QuickDraw GX applications.
- It contains all of the required calls to use the GX routines and QuickDraw
- together. It can put up one or more windows and also supports printing.
-
- The application is expected to supply the following functions which are
- called by the shell:
- void DoSetup(void);
- void DoDraw(WindowPtr);
- OSErr DoNew(void);
- void DoClose(WindowPtr);
- void DoIdle(WindowPtr);
- void DoTeardown(void);
- void DoClick(WindowPtr, Point);
-
- Written by: Developer Technical Support
-
- Copyright: © 1992-1997 by Apple Computer, Inc., all rights reserved.
-
- Writers:
-
- (DMH) Dave Hersey
- (IK) Ingrid Kelly
-
- Change History (most recent first):
-
- <2> 6/1/97 IK Modified printing routines for GXGraphics 1.1.6.
- <1> 9/1/92 DMH First created.
- */
-
- #include <CodeFragments.h>
- #include <Fonts.h>
- #include <Gestalt.h>
- #include <Resources.h>
-
- #include "GraphicsLibraries.h"
- #include "PrintingLibrary.h"
-
- #include "main.h"
-
- /* ---------------------------------------------------------------------------
- Global variables
- --------------------------------------------------------------------------- */
-
- short gAppResRefNum;
-
- /* gGraphicsHeapSize sets the size of the graphics gxHeap created by
- calling the GXNewGraphicsClient routine. You can determine the amount of
- graphics gxHeap required by using GraphicsBug.
- */
- long gGraphicsHeapSize = 512;
-
- gxGraphicsClient gGXGraphicsClient;
-
- /* gOurPrintingOverrideUPP is a universal proc pointer for the printing
- event override. This is so that the override can be native PowerPC code.
- */
- GXPrintingEventUPP gGXPrintingEventUPP;
- GXFormatDialogUPP gGXFormatDialogUPP;
-
- Boolean gQuitting = false;
- long gSleep = 0;
-
- #if defined(powerc) && !defined(__MWERKS__)
- QDGlobals qd;
- #endif
-
- /* ---------------------------------------------------------------------------
- main
- --------------------------------------------------------------------------- */
-
- void main()
- {
- CursHandle theCurs;
- Handle menuBar;
- WindowPtr wind;
-
- gAppResRefNum = CurResFile();
-
- /* Initialize the Mac OS Toolbox. */
-
- InitToolbox();
-
- theCurs = GetCursor(watchCursor);
- SetCursor(*theCurs);
-
- /* Create the menu bar. */
-
- menuBar = GetNewMBar(rMenuBar);
- if ( menuBar == nil )
- goto GetNewMBarFailed;
-
- SetMenuBar(menuBar);
- DisposeHandle(menuBar);
-
- /* Initialize the QuickDraw GX environment. */
-
- if ( !InitializeGXEnvironment() )
- {
- SetCursor(&qd.arrow);
- (void)StopAlert(rNoQuickDrawGXALRTID, NULL);
- return;
- }
-
- /* Before starting to draw, we'll call the DoSetup routine so we can
- initialize any global variables or behaviors. This routine would be a good
- time to change the gGraphicsHeapSize variable before we make the new
- graphics client next.
- */
- DoSetup();
-
- /* Initialize the other managers, if no errors occurred. */
-
- {
- gQuitting = false;
- AddResMenu(GetMHandle(mApple), 'DRVR'); /* add Apple Menu items. */
- DrawMenuBar();
-
- DoNew();
-
- SetCursor(&qd.arrow);
-
- while (!gQuitting)
- EventLoop();
-
- /* Leaving. Close all the windows so we get rid of any data we or GX
- created. Then, dispose of the common colors and exit the GX printing and
- graphics environment.
- */
- while ( (wind = FrontWindow()) != NULL )
- DoClose(wind);
- }
-
- GetNewMBarFailed:
- TerminateGXEnvironment(); /* Exit the QuickDraw GX environment. */
- DoTeardown(); /* Dispose of any global variables we made in DoSetup. */
- }
-
- /* ---------------------------------------------------------------------------
- InitToolbox
- --------------------------------------------------------------------------- */
-
- void InitToolbox (void)
- {
- /* Generic heap initialization. */
-
- MaxApplZone();
- MoreMasters(); MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters(); MoreMasters();
-
- /* Start up the toolbox so we can notify people if there's a problem */
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
- }
-
- /* ---------------------------------------------------------------------------
- InitializeGXEnvironment
- Check to see if QuickDraw GX is installed. If not, alert the user...
- --------------------------------------------------------------------------- */
-
- Boolean InitializeGXEnvironment(void)
- {
- long theFeature;
- Boolean debuggingInitInstalled = false;
-
- /* Before making any calls, check if QuickDraw GX is available. */
-
- if ( Gestalt(gestaltGraphicsVersion, &theFeature) != noErr )
- return false;
-
- #ifdef powerc
- /* This is a sanity check to see if the QuickDrawGXLib shared library is
- installed. If the application is "weak" linked to the library, the
- Process Manager will launch it even if the library is missing,
- although the Code Fragment Manager will not resolve the addresses of
- the functions in the library. If the application calls any of these
- functions, it will crash.
-
- The application should check if any of the QuickDraw GX functions
- cannot be resolved and exit gracefully if the library is missing. Note
- that the check could be performed against any function the application
- calls in the library. GXNewGraphicsClient is often the first function that
- is called from the library, so it is convenient.
- */
- if ( (UInt32)GXNewGraphicsClient == kUnresolvedCFragSymbolAddress )
- return false;
- #endif
-
- /* The gestaltGraphicsAttr Gestalt attribute can be used to determine if the
- GXGraphicsLib has been loaded, the GX debugging extension is
- installed, or the PowerPC version version of GX is running. In our case,
- we only need to know if the debugging extension was installed. If it is,
- we will enable the GX validation and notice handling features. We define
- debuggingInitInstalled as true to enable GX validation and notice handler
- within the SetGXDebuggingWorld function.
- */
- if ( Gestalt(gestaltGraphicsAttr, &theFeature) == noErr )
- if ( (theFeature & gestaltGraphicsIsDebugging) == gestaltGraphicsIsDebugging )
- debuggingInitInstalled = true;
-
- /* The GXNewGraphicsClient routine defines the graphics heap size. If you do
- not make this call, the GX graphics engine will create this heap
- automatically. How? It will create a heap which is a percentage of your
- application's ideal memory foot print. This call allows you to explicity
- define the ammount of memory used by the graphics system for its graphics
- objects heap.
- */
- gGXGraphicsClient = GXNewGraphicsClient(nil, gGraphicsHeapSize * 1024, 0L);
-
- /* After we attempted to create the graphics client, we need to determine if
- the call succeeded. If the call did not (as in the case for all GX
- functions), gGXGraphicsClient will be nil. If it is, we alert the user to
- the problem. Otherwise, we will attempt to allocate the GX heap below.
- */
- if ( gGXGraphicsClient == nil )
- return false;
-
- /* Initialize the new graphics environment and create the GX heap. */
-
- GXEnterGraphics();
-
- /* Calling GXEnterGraphics allocates the memory within the GX heap. The only
- reason the call would not succeed is if there is not enough memory. In
- this case, the graphics error which will be posted is -27999 (out of
- memory). At this point, we have not installed an error handler, so we
- check for the error number corresponding to the out of memory error.
- */
- if ( GXGetGraphicsError(nil) != -27999 )
- {
- SetGXDebuggingWorld(debuggingInitInstalled);
-
- /* Initialize the printing features within QuickDraw GX. */
-
- (void)GXPrInitPrinting();
-
- #if 0
- /* Force the spooling functions of the PrintingLibrary to convert each
- page shape to a QuickDraw picture before passing the compressed
- QuickDraw GX data to QuickTime for decompression.
- */
- GXPrSetPrintingOptions(GXPrGetPrintingOptions() | kUseShapeToQuickDrawPictureConversion);
- #endif
-
- /* Initialize the printing event override for the QuickDraw GX printing engine. */
-
- gGXPrintingEventUPP = NewGXPrintingEventProc(GXPrintingEventOverride);
-
- /* Initialize the custom page format handler. */
-
- gGXFormatDialogUPP = NewGXFormatDialogProc(GXFormatDialogOverride);
-
- /* We initialize the CommonColors Library. This will allow us to set the
- color of a shape by calling the SetShapeCommonColor function. We will need
- to call DisposeCommonColors when we leave, to clean up the world.
- */
- InitCommonColors();
- }
- else
- {
- /* Since we can not allocate the requested size for our GX heap, we need to
- throw away the client we created and alert the user that there is not
- enough memory to continue.
-
- However, you could try to create a smaller GX heap. If you decide to try
- to create a smaller GX heap which would meet the needs of your
- application, you need to dispose of the client you had originally created.
- Why? The original client contains the GX heap size requested, which was
- too big, therefore you need to dispose of it and create a client
- requesting a smaller size and call GXEnterGraphics and check for an error.
- */
- GXDisposeGraphicsClient(gGXGraphicsClient);
- DebugStr ("\pInsufficient memory for QuickDraw GX.");
- }
-
- return true;
- }
-
- /* ---------------------------------------------------------------------------
- TerminateGXEnvironment
- --------------------------------------------------------------------------- */
-
- void TerminateGXEnvironment(void)
- {
- /* Deallocate all of the default QuickDraw GX data structures. */
-
- DisposeCommonColors();
-
- DisposeRoutineDescriptor(gGXFormatDialogUPP);
- DisposeRoutineDescriptor(gGXPrintingEventUPP);
- GXPrExitPrinting();
-
- GXExitGraphics();
- GXDisposeGraphicsClient(gGXGraphicsClient);
- }
-
- /* ---------------------------------------------------------------------------
- SetGXDebuggingWorld
- This function enables the GX error handling capabilities and validation
- routines. The validation routines are only enabled if the user has
- installed the QuickDraw GX debugging extension. These routines are not
- available with the non-debugging extension. Calling them when they are not
- installed will not cause any problems, but it will cause unnecessary work
- to be done by your application and the GX dispatcher.
- --------------------------------------------------------------------------- */
-
- void SetGXDebuggingWorld(Boolean debuggingInitInstalled)
- {
- /* Set QuickDraw GX validation routines if the user has installed the
- QuickDraw GX debugging extension. gxPublicValidation will check parameters
- to public routines. This validation setting is the recommended setting
- while you are developing your GX application. For additional details
- regarding the various levels of validation, see "Inside Macintosh:
- QuickDraw GX Environment and Utilities." As you increase the amount of
- validation, the drawing speed will SLOW down due to all of the internal
- checking.
- */
- if ( debuggingInitInstalled )
- GXSetValidation(gxPublicValidation + gxTypeValidation);
-
- /* Calling SetGraphicsLibraryErrors will install a GX error and warning
- handler. This call is provided by the QuickDraw GX graphics debugging
- library. Any time a GX error or warning is generated, it will be posted to
- Macsbug.
- */
- SetGraphicsLibraryErrors();
-
- /* If the user has installed the GX debugging version, we install a notice
- handler. Why? The GX notice handling capabilities are only available with
- the debugging version.
- */
- if ( debuggingInitInstalled )
- SetGraphicsLibraryNotices();
- }
-
- /* ---------------------------------------------------------------------------
- GXPrintingEventOverride
- Override for GXPrintingEvent. It allows us to update our windows when the
- moveable modal printing dialogs are moved. Keep this in your main
- segment, which will always be loaded when GX tries to call the routine.
- --------------------------------------------------------------------------- */
-
- OSErr GXPrintingEventOverride(EventRecord *event, Boolean filterEvent)
- {
- if ( !filterEvent )
- DoEvent(event); /* generic event handler */
-
- return noErr;
- }
-
- /* ---------------------------------------------------------------------------
- GXFormatDialogOverride
- --------------------------------------------------------------------------- */
-
- OSErr GXFormatDialogOverride(gxFormat format, StringPtr title, gxDialogResult *result)
- {
- Collection formatCollection;
- T_CustomCollection customConfig;
- gxPanelSetupRecord panelInfo;
- OSErr error;
-
- if ( format == nil )
- return paramErr;
-
- formatCollection = GXGetFormatCollection(format);
- error = GXGetJobError(GXGetFormatJob(format));
- if ( error != noErr )
- goto GXGetFormatCollectionFailed;
-
- error = GetCollectionItem(formatCollection, kCustomCollectionType, gxPrintingTagID, nil, &customConfig);
- if ( error == collectionItemNotFoundErr )
- {
- customConfig.one = false;
- customConfig.two = false;
- customConfig.three = 0;
-
- error = AddCollectionItem(formatCollection,
- kCustomCollectionType,
- gxPrintingTagID,
- sizeof(T_CustomCollection),
- &customConfig);
- }
-
- if ( error == noErr )
- {
- panelInfo.panelKind = gxApplicationPanel;
- panelInfo.panelResId = rFormatPanelDITLID;
- panelInfo.resourceRefNum = gAppResRefNum;
- panelInfo.refCon = 0;
-
- error = GXSetupDialogPanel(&panelInfo);
- }
-
- GXGetFormatCollectionFailed:
- error = Forward_GXFormatDialog(format, title, result);
-
- return error;
- }
-